home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.10 Oct 94 / Sprocket / Lib / Window.cp < prev   
Encoding:
Text File  |  1994-08-27  |  20.9 KB  |  851 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.                 
  11.     Written by: Dave Falkenburg
  12.  
  13.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  14.  
  15.     Change History (most recent first):
  16.     
  17.          <2>     8/27/94    DRF        In TWindow::Close, call window’s (de)Activate method before
  18.                                     closing so that menus can be properly updated.
  19.     
  20.     To Do:        Make sure invisible windows can be created & managed
  21.                 Handle modal windows as another class of windows
  22.                 Fix activate bugs when showing and hiding windows
  23.                 Window positioning methods (getters and setters)
  24.                 Display Manager support
  25.                 Changes to support AEObject model
  26.  */
  27.  
  28. #include <Types.h>
  29. #include <Windows.h>
  30. #include <Errors.h>
  31. #include <Script.h>        //    for GetMBarHeight()
  32. #include <LowMem.h>        //    for LMGetWindowList()
  33.  
  34. #include "AppLib.h"
  35. #include "Window.h"
  36.  
  37. const short            kFloatingWindowKind        = 1000;
  38. const short            kNormalWindowKind        = 1001;
  39. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  40.  
  41. const short            kScreenEdgeSlop            = 4;
  42. const short            kSpaceForFinderIcons    = 64;
  43. const short            kMinimumTitleBarHeight    = 21;
  44. const short            kMinimumWindowSize        = 32;
  45.  
  46. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  47.  
  48. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  49. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  50.  
  51.  
  52. struct    CalcWindowAreaDeviceLoopUserData
  53.     {
  54.     GDHandle    fScreenWithLargestPartOfWindow;
  55.     long        fLargestArea;
  56.     Rect        fWindowBounds;
  57.     };
  58.  
  59.  
  60.  
  61.  
  62.  
  63. TWindow::TWindow()
  64.     {
  65.     }
  66.  
  67.  
  68. TWindow::~TWindow()
  69.     {
  70.     }
  71.  
  72.  
  73. void
  74. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  75.     {
  76.     WindowPtr    behindWindow,oldFrontMostWindow;
  77.     
  78.     if (typeOfWindowToCreate == kModalWindow)
  79.         {
  80.         DebugStr("\pModal windows aren’t supported yet");
  81.         fWindowType = kFloatingWindow;
  82.         return;
  83.         }
  84.     else if (typeOfWindowToCreate == kFloatingWindow)
  85.         {
  86.         behindWindow = (WindowPtr) -1;
  87.         oldFrontMostWindow = FrontWindow();
  88.  
  89.         fWindowType = kFloatingWindow;
  90.         }
  91.     else if (typeOfWindowToCreate == kNormalWindow)
  92.         {
  93.         behindWindow = LastFloatingWindow();
  94.  
  95.         fWindowType = kNormalWindow;
  96.         
  97.         if (behindWindow == kNoFloatingWindows)
  98.             oldFrontMostWindow = nil;
  99.         else
  100.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  101.         }
  102.  
  103.     fWindow = this->MakeNewWindow(behindWindow);
  104.     fIsVisible = ((WindowPeek) fWindow)->visible;
  105.  
  106.     if (fWindow)
  107.         {
  108.         SetWRefCon(fWindow,(long) this);
  109.  
  110.         if (typeOfWindowToCreate == kModalWindow)
  111.             {
  112.             DebugStr("\pCan’t create Modal windows yet");
  113.             }
  114.         else if (typeOfWindowToCreate == kFloatingWindow)
  115.             {
  116.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  117.             
  118.             //    make sure the other window stays hilited
  119.             if (oldFrontMostWindow)
  120.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  121.             }
  122.         else if (typeOfWindowToCreate == kNormalWindow)
  123.             {
  124.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  125.  
  126.             //    unhighlight the old front window
  127.             if (oldFrontMostWindow)
  128.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  129.  
  130.             //    hilite the new window…
  131.             HiliteAndActivateWindow(fWindow,true);
  132.             }
  133.         }
  134.     }
  135.  
  136.  
  137. void
  138. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  139.     {
  140.     }
  141.  
  142. void
  143. TWindow::Idle(EventRecord * /* anEvent */)
  144.     {
  145.     }
  146.     
  147. void
  148. TWindow::Activate(Boolean /* activating */)
  149.     {
  150.     }
  151.     
  152. void
  153. TWindow::Draw(void)
  154.     {
  155.     }
  156.     
  157. void
  158. TWindow::Click(EventRecord * /* anEvent */)
  159.     {
  160.     }
  161.     
  162. void
  163. TWindow::KeyDown(EventRecord * /* anEvent */)
  164.     {
  165.     }
  166.  
  167.  
  168. void
  169. TWindow::Select(void)
  170.     {
  171.     WindowPtr    currentFrontWindow;
  172.     
  173.     if (fWindowType == kFloatingWindow)
  174.         currentFrontWindow = FrontWindow();
  175.     else if (fWindowType == kNormalWindow)
  176.         currentFrontWindow = FrontNonFloatingWindow();
  177.     else
  178.         {
  179.         }
  180.  
  181.     if (currentFrontWindow != fWindow)
  182.         {
  183.         if (fWindowType == kFloatingWindow)
  184.             BringToFront(fWindow);
  185.         else
  186.             {
  187.             WindowPtr    lastFloater = LastFloatingWindow();
  188.  
  189.             //    If there are no floating windows,
  190.             //    just call SelectWindow like the good ol’ days
  191.  
  192.             if (lastFloater == kNoFloatingWindows)
  193.                 SelectWindow(fWindow);
  194.             else
  195.                 {
  196.                 // Deactivate the window currently in front.
  197.  
  198.                 HiliteAndActivateWindow(currentFrontWindow,false);
  199.     
  200.                 // Bring it behind the last floating window and activate it.
  201.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  202.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  203.                 // this is no longer necessary.
  204.  
  205.                 SendBehind(fWindow,lastFloater);
  206.                 HiliteAndActivateWindow(fWindow,true);
  207.                 }
  208.             }
  209.         }
  210.     }
  211.  
  212.  
  213. void
  214. TWindow::Drag(Point startPoint)
  215.     {
  216.     GrafPtr        savePort;
  217.     KeyMap        theKeyMap;
  218.     Boolean        commandKeyDown = false;
  219.     RgnHandle    draggingRegion;
  220.     long        dragResult;
  221.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  222.     
  223.     if (WaitMouseUp())        //    de-bounce?
  224.         {
  225.         // Set up the Window Manager port.
  226.     
  227.         GetPort(&savePort);
  228.         SetPort(gWindowManagerPort);
  229.         SetClip(GetGrayRgn());
  230.  
  231.         // Check to see if the command key is down.
  232.     
  233.         GetKeys(theKeyMap);
  234.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  235.         
  236.         if (commandKeyDown)
  237.             {
  238.             //    We’re not going to change window ordering,
  239.             //    so make sure that we don’t drag in front of
  240.             //    other windows which may be in front of ours.
  241.  
  242.             ClipAbove(windowAsWindowPeek);
  243.             }
  244.         else if (fWindowType != kFloatingWindow)
  245.             {
  246.             //    We’re dragging a normal window, so make sure
  247.             //    that we don’t drag in front of any floating
  248.             //    windows.
  249.  
  250.             ClipAbove((WindowPeek) FrontNonFloatingWindow());
  251.             }
  252.         
  253.         //    Drag an outline of the window around the desktop.
  254.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  255.  
  256.         draggingRegion = NewRgn();
  257.         CopyRgn(windowAsWindowPeek->strucRgn,draggingRegion);
  258.         dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  259.         DisposeRgn(draggingRegion);
  260.  
  261.     
  262.         SetPort(savePort);    //    Get back to old port
  263.  
  264.         if ((dragResult != 0) && (dragResult != 0x80008000))
  265.             {
  266.             this->Nudge((dragResult & 0xFFFF),(dragResult >> 16));
  267.             }
  268.         }
  269.  
  270.     if (!commandKeyDown)
  271.         Select();
  272.     }
  273.  
  274. void
  275. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  276.     {
  277.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  278.     short        newHorizontalPosition,newVerticalPosition;
  279.     
  280.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  281.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  282.  
  283.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  284.     }
  285.  
  286. void
  287. TWindow::Grow(Point startPoint)
  288.     {
  289.     GrafPtr    oldPort;
  290.     long    newSize;
  291.     Rect    oldWindowRect,resizeLimits;
  292.     
  293.     GetPort(&oldPort);
  294.     
  295.     GetWindowSizeLimits(&resizeLimits);
  296.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  297.     if (newSize)
  298.         {
  299.         oldWindowRect = fWindow->portRect;
  300.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  301.         SetPort(fWindow);
  302.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  303.         }
  304.     
  305.     SetPort(oldPort);
  306.     }
  307.  
  308.  
  309. void
  310. TWindow::Zoom(short zoomState)
  311.     {
  312.     GrafPtr        oldPort;
  313.     FontInfo    systemFontInfo;
  314.     short        titleBarHeight;
  315.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  316.     short        amountOffscreen;
  317.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  318.     GDHandle    bestDevice;
  319.     
  320.     GetPort(&oldPort);
  321.  
  322.     //    Figure out the height of the title bar so we can properly position
  323.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  324.     //
  325.     //    This probably isn’t the best thing to do: A better way might be 
  326.     //    to diff the structure and content region rectangles?
  327.  
  328.     SetPort(gWindowManagerPort);
  329.     GetFontInfo(&systemFontInfo);
  330.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  331.     if ((titleBarHeight % 2) == 1)
  332.         titleBarHeight--;
  333.     if (titleBarHeight < kMinimumTitleBarHeight)
  334.         titleBarHeight = kMinimumTitleBarHeight;
  335.  
  336.  
  337.     //    Only do the voodoo magic if we are really “zooming” the window.
  338.  
  339.     if (zoomState == inZoomOut)
  340.         {
  341.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  342.         bestScreenRect.top += titleBarHeight;
  343.  
  344.         this->GetPerfectWindowSize(&perfectWindowRect);
  345.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  346.  
  347.         //    Take the zero-pined perfect window size and move it to
  348.         //    the top left of the    window’s content region.
  349.  
  350.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  351.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  352.  
  353.         
  354.         //    Does perfectWindowRect fit completely on the best screen?
  355.         
  356.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  357.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  358.             {
  359.             //    SectRect sez perfectWindowRect doesn’t completely fit
  360.             //    on the screen, so bump the window so that more of it fits.
  361.  
  362.             //    Make sure that the left edge of perfectWindowRect is forced
  363.             //    onto the best screen.  This is in case we are bumping
  364.             //    the window to the right.
  365.  
  366.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  367.             if (amountOffscreen > 0)
  368.                 {
  369.                 perfectWindowRect.left += amountOffscreen;
  370.                 perfectWindowRect.right += amountOffscreen;
  371.                 }
  372.  
  373.             //    Make sure that the left edge of perfectWindowRect is forced
  374.             //    onto the best screen.  This is in case we are bumping
  375.             //    the window downward to a new screen.
  376.     
  377.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  378.             if (amountOffscreen > 0)
  379.                 {
  380.                 perfectWindowRect.top += amountOffscreen;
  381.                 perfectWindowRect.bottom += amountOffscreen;
  382.                 }
  383.  
  384.             //    If right edge of window falls off the screen,
  385.             //        Move window to the left until the right edge IS on the screen
  386.             //        OR the left edge is at bestScreenRect.left
  387.  
  388.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  389.             if (amountOffscreen > 0)
  390.                 {
  391.                 //    Are we going to push the left edge offscreen? If so, change the
  392.                 //    offset so we move the window all the way over to the left.
  393.                 
  394.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  395.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  396.  
  397.                 perfectWindowRect.left -= amountOffscreen;
  398.                 perfectWindowRect.right -= amountOffscreen;
  399.                 }
  400.  
  401.             //    If bottom edge of window falls off the screen,
  402.             //        Move window to up until the bottom edge IS on the screen
  403.             //        OR the top edge is at bestScreenRect.top
  404.  
  405.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  406.             if (amountOffscreen > 0)
  407.                 {
  408.                 //    Are we going to push the top edge offscreen? If so, change the
  409.                 //    offset so we move the window just to the top.
  410.                 
  411.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  412.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  413.  
  414.                 perfectWindowRect.top -= amountOffscreen;
  415.                 perfectWindowRect.bottom -= amountOffscreen;
  416.                 }
  417.  
  418.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  419.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  420.                 {
  421.                 //    The edges of the window still fall offscreen,
  422.                 //    so make the window smaller until it fits.
  423.                 
  424.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  425.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  426.  
  427.                 //    If the right edge is still falling off,
  428.                 //        save space for Finder’s disk icons as well.
  429.  
  430.                 if (perfectWindowRect.right > bestScreenRect.right)
  431.                     {
  432.                     perfectWindowRect.right = bestScreenRect.right;
  433.                     
  434.                     //    If we were on the main screen, leave room for Finder icons, too.
  435.                     
  436.                     if (bestDevice == GetMainDevice())
  437.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  438.                     }
  439.                 }
  440.             }
  441.  
  442.         //    Stash our new rectangle inside of the Window’s dataHandle
  443.         //    so that ZoomWindow does the right thing.
  444.         
  445.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  446.         }
  447.  
  448.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  449.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  450.     
  451.     SetPort(fWindow);
  452.  
  453.     Rect    oldWindowRect = fWindow->portRect;
  454.     
  455.     ZoomWindow(fWindow,zoomState,false);
  456.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  457.  
  458.     SetPort(oldPort);
  459.     }
  460.  
  461. void
  462. TWindow::ShowHide(Boolean showFlag)
  463.     {
  464.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  465.     //    the global function, and not the method ShowHide. Unintended recursion
  466.     //    can do bad things to the unsuspecting programmer.
  467.     
  468.     //    Some C++ programmers would always prepend the “::” on function calls.
  469.     
  470.     ::ShowHide(fWindow,showFlag);
  471.     fIsVisible = showFlag;
  472.     }
  473.     
  474.  
  475. Boolean
  476. TWindow::EventFilter(EventRecord * /* theEvent */)
  477.     {
  478.     return false;
  479.     }
  480.     
  481.  
  482. void
  483. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  484.     {
  485.     *perfectSize = qd.screenBits.bounds;
  486.     }
  487.  
  488. void
  489. TWindow::GetWindowSizeLimits(Rect *limits)
  490.     {
  491.     limits->top = limits->left = kMinimumWindowSize;
  492.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  493.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  494.     }
  495.  
  496. void
  497. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  498.     {
  499.     }
  500.  
  501.  
  502. Boolean
  503. TWindow::IsVisible(void)
  504.     {
  505.     return fIsVisible;
  506.     }
  507.  
  508.  
  509. Boolean
  510. TWindow::CanClose(void)
  511.     {
  512.     return true;
  513.     }
  514.  
  515.  
  516. Boolean
  517. TWindow::Close(void)
  518.     {
  519.     WindowPtr    newFrontWindow = nil;
  520.     
  521.     if (FrontNonFloatingWindow() == fWindow)
  522.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  523.  
  524.     this->Activate(false);
  525.     DisposeWindow(fWindow);
  526.  
  527.     if (newFrontWindow)
  528.         HiliteAndActivateWindow(newFrontWindow,true);
  529.  
  530.     return true;
  531.     }
  532.  
  533.  
  534. Boolean
  535. TWindow::DeleteAfterClose(void)
  536.     {
  537.     return true;
  538.     }
  539.  
  540.  
  541. void
  542. TWindow::DoEditMenu(short /* menuCode */)
  543.     {
  544.     }
  545.  
  546.  
  547. OSErr
  548. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  549.     {
  550.     OSErr    result = dragNotAcceptedErr;
  551.     
  552.     switch (dragMessage)
  553.         {
  554.         case    dragTrackingEnterWindow:
  555.             result = this->DragEnterWindow(theDrag);
  556.             break;
  557.         
  558.         case    dragTrackingInWindow:
  559.             result = this->DragInWindow(theDrag);
  560.             break;
  561.             
  562.         case    dragTrackingLeaveWindow:
  563.             result = this->DragLeaveWindow(theDrag);
  564.             break;
  565.             
  566.         default:
  567.             break;
  568.         }
  569.  
  570.     return result;
  571.     }
  572.  
  573.  
  574. OSErr
  575. TWindow::DragEnterWindow(DragReference /* theDrag */)
  576.     {
  577.     return dragNotAcceptedErr;
  578.     }
  579.  
  580.  
  581. OSErr
  582. TWindow::DragInWindow(DragReference /* theDrag */)
  583.     {
  584.     return dragNotAcceptedErr;
  585.     }
  586.  
  587.  
  588. OSErr
  589. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  590.     {
  591.     return dragNotAcceptedErr;
  592.     }
  593.     
  594.  
  595. OSErr
  596. TWindow::HandleDrop(DragReference /* theDrag */)
  597.     {
  598.     return dragNotAcceptedErr;
  599.     }
  600.  
  601.  
  602. ///////////////////////////////////////////////////////////////////////////
  603. //
  604. //    Utility Functions used for floating windows
  605. //
  606.  
  607. TWindow *
  608. GetWindowObject(WindowPtr aWindow)
  609.     {
  610.     short    wKind;
  611.     
  612.     if (aWindow != nil)
  613.         {
  614.         wKind = ((WindowPeek) aWindow)->windowKind;
  615.  
  616.         if (wKind >= userKind)
  617.             {
  618.             //    All windowKinds >= userKind are based upon TWindow
  619.  
  620.             return (TWindow *) GetWRefCon(aWindow);
  621.             }
  622.         }
  623.     return (TWindow *) nil;
  624.     }
  625.  
  626.  
  627. ////////////////////////////////////////////////////////////////////////
  628. //
  629. //    Utility functions
  630.  
  631.  
  632. pascal WindowPtr
  633. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  634.     {
  635.     if (gHasColorQuickdraw)
  636.         return GetNewCWindow(windowID,wStorage,behind);
  637.     else
  638.         return GetNewWindow(windowID,wStorage,behind);
  639.     }
  640.  
  641.  
  642. pascal WindowPtr
  643. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  644.     {
  645.     if (gHasColorQuickdraw)
  646.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  647.     else
  648.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  649.     }
  650.  
  651.  
  652. WindowPtr
  653. LastFloatingWindow(void)
  654.     {
  655.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  656.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  657.     
  658.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  659.         {
  660.         if (aWindow->visible)
  661.             lastFloater = (WindowPtr) aWindow;
  662.  
  663.         aWindow = (WindowPeek) aWindow->nextWindow;
  664.         }
  665.     return(lastFloater);
  666.     }
  667.  
  668.  
  669. WindowPtr
  670. FrontNonFloatingWindow(void)
  671.     {
  672.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  673.  
  674.     //    Skip over floating windows
  675.         
  676.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  677.         aWindow = (WindowPeek) aWindow->nextWindow;
  678.  
  679.     //    Skip over invisible, but otherwise normal windows
  680.     
  681.     while (aWindow && (aWindow->visible == 0))
  682.         aWindow = (WindowPeek) aWindow->nextWindow;
  683.         
  684.     return (WindowPtr) aWindow;
  685.     }
  686.  
  687.  
  688. void
  689. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  690.     {
  691.     GrafPtr        oldPort;
  692.     TWindow    *    wobj = GetWindowObject(aWindow);
  693.     
  694.     if (aWindow)
  695.         {
  696.         HiliteWindow(aWindow,active);
  697.  
  698.         if (wobj != nil)
  699.             {
  700.             GetPort(&oldPort);
  701.             SetPort(aWindow);
  702.             wobj->Activate(active);
  703.             SetPort(oldPort);
  704.             }    
  705.         }
  706.     }
  707.  
  708. void
  709. SuspendResumeWindows(Boolean resuming)
  710.     {
  711.     //    When we suspend/resume, hide/show all the visible floaters
  712.     
  713.     HiliteShowHideFloatingWindows(resuming,true);
  714.     }
  715.  
  716. void
  717. HiliteWindowsForModalDialog(Boolean hiliting)
  718.     {
  719.     //    When we display a modal dialog, we need to unhighlight
  720.     //    all visible floaters. We also need to re-hilite them
  721.     //    afterwards.
  722.     
  723.     HiliteShowHideFloatingWindows(hiliting,false);
  724.     }
  725.  
  726. void
  727. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  728.     {
  729.     WindowPeek    aWindow;
  730.     TWindow *    wobj;
  731.     
  732.     HiliteAndActivateWindow(FrontNonFloatingWindow(),hiliting);
  733.  
  734.     aWindow = LMGetWindowList();
  735.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  736.         {
  737.         wobj = GetWindowObject((WindowPtr) aWindow);
  738.         
  739.         //    If we are hiding or showing, only hide/show windows
  740.         //    that were visible to begin with.
  741.         
  742.         //    NOTE:    We use our copy of the visible flag so we can
  743.         //            automatically show floaters on a resume event.
  744.         
  745.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  746.         //            really need the “::” on ShowHide, but as long
  747.         //            as we’re trying to avoid ambiguity.
  748.         
  749.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  750.             ::ShowHide((WindowPtr) aWindow,hiliting);
  751.             
  752.         //    All floaters are hilited if any floater is hilited
  753.  
  754.         HiliteWindow((WindowPtr) aWindow,hiliting);
  755.         aWindow = (WindowPeek) aWindow->nextWindow;
  756.         }
  757.     }
  758.  
  759.  
  760. ///////////////////////////////////////////////////////////////////////////
  761. //
  762. //    Routines used for dealing with windows and multiple screens
  763. //
  764.  
  765. pascal void
  766. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  767.     {
  768.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  769.     long                                windowAreaOnThisScreen;
  770.     Rect                                windowRectOnThisScreen;
  771.     
  772.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  773.  
  774.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  775.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  776.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  777.  
  778.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  779.         {
  780.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  781.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  782.         }
  783.     }
  784.  
  785.  
  786. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  787.  
  788.  
  789. void
  790. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  791.     {
  792.     RgnHandle                            copyOfWindowStrucRgn;
  793.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  794.  
  795.     //    Use DeviceLoop to find out what GDevice contains the largest
  796.     //    portion of the supplied window.
  797.     //
  798.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  799.     //            the window strucRgn, not contRgn.
  800.  
  801.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  802.     deviceLoopData.fLargestArea = -1;
  803.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  804.     
  805.     copyOfWindowStrucRgn = NewRgn();
  806.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  807.  
  808.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  809.  
  810.     DisposeRgn(copyOfWindowStrucRgn);
  811.     
  812.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  813.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  814.  
  815.     //    Leave some space around the edges of the screen so window look good, AND
  816.     //    if the best device is the main screen, leave space for the Menubar
  817.     
  818.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  819.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  820.         theBestScreenRect->top += GetMBarHeight();
  821.     }
  822.  
  823.  
  824. ///////////////////////////////////////////////////////////////////////////
  825. //
  826. //    Drag Manager callback routines which dispatch to a window’s method
  827. //
  828.  
  829. pascal OSErr
  830. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  831.     {
  832.     TWindow *wobj = GetWindowObject(theWindow);
  833.     
  834.     if (wobj)
  835.         return(wobj->HandleDrag(dragMessage,theDrag));
  836.     else
  837.         return dragNotAcceptedErr;
  838.     }
  839.  
  840.     
  841. pascal OSErr
  842. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  843.     {
  844.     TWindow *wobj = GetWindowObject(theWindow);
  845.     
  846.     if (wobj)
  847.         return(wobj->HandleDrop(theDrag));
  848.     else
  849.         return dragNotAcceptedErr;
  850.     }
  851.